import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class Login extends JFrame implements ActionListener{

	 JLabel l_pw;
	 JPasswordField t_pw;
	 JButton login;
	 String driver="com.mysql.jdbc.Driver";
	 String url="jdbc:mysql://localhost:3306/gage";
	 String uid="root";
	 String upasswd="apmsetup";
	
	 Connection con;
	 PreparedStatement pstmt;
	 ResultSet rs;
 
	
	public Login(){
		
       super("로그인");
       l_pw= new JLabel("비밀번호");
		   t_pw = new JPasswordField(10);
		   login = new JButton("Login");
		  
		   getContentPane().setLayout(new FlowLayout());
		   add(l_pw);
		   add(t_pw);
		   add(login);
		    
		   login.addActionListener(this);
		  
		   setBounds(200,100,300,80); 
		   setVisible(true);
		   connect();		  
		 }
		 
		 public boolean connect() {
		  boolean result=false;
		  try {
		   Class.forName(driver);
		   con = DriverManager.getConnection(url,uid,upasswd);
		   if(con!=null){
		    result= true;
		   }else{
		    result= false;
		   }
		  } catch (ClassNotFoundException e) {  
		   e.printStackTrace();
		  } catch (SQLException e) {
		   e.printStackTrace();
		  }
		  return result;  
		 }
		 
		 public void loginCheck(){
		  
		  char[] ch=t_pw.getPassword();
		  String passStr=new String(ch);
		  if(passStr.length()==0){
		   showMsg("비밀번호를 입력하지 않았습니다.");
		   t_pw.requestFocus();
		   return;
		  }

		  String sql="select * from admin where pw=?";
		  try {
		   pstmt=con.prepareStatement(sql);
		   pstmt.setString(1, passStr);
		   rs=pstmt.executeQuery();
		   
		   if(rs.next()){
        Member m = new Member(this);
		    this.setVisible(false);
		 
		   }else{
		    showMsg("비밀번호가 맞지 않습니다.");
		   }
		  } catch (SQLException e) {
		   e.printStackTrace();
		  }
		  
		 }
		 
		 
		 public void actionPerformed(ActionEvent e) {
		  loginCheck();
		 }
		 
		 
		 public void showMsg(String msg){
		  JOptionPane.showMessageDialog(getParent(), msg);
		  
		 }
		 
		 public static void main(String[] args) {
		  new Login();
		 }
	
		}
